//日期转化为  xx-xx

export function formatDate(dateString) {
    const date = new Date(dateString);
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    return `${year}-${month}`;
  }


export function getWeekRange(dateStr) {
  const date = new Date(dateStr);
  // 获取当前日期是星期几，0 代表周日，1 - 6 代表周一到周六
  const dayOfWeek = date.getDay();
  // 计算距离周一的偏移量，周日时偏移量为 6，其他情况为当前星期几减 1
  const offsetToMonday = dayOfWeek === 0 ? 6 : dayOfWeek - 1;
  // 计算周一的日期
  const startDate = new Date(date.getTime() - offsetToMonday * 24 * 60 * 60 * 1000);
  // 计算周日的日期
  const endDate = new Date(startDate.getTime() + 6 * 24 * 60 * 60 * 1000);

  const formatDate = (d) => {
    const year = d.getFullYear();
    const month = String(d.getMonth() + 1).padStart(2, '0');
    const day = String(d.getDate()).padStart(2, '0');
    return `${year}-${month}-${day-1}`;
  };

  return {
    start: formatDate(startDate),
    end: formatDate(endDate)
  };
}